﻿using System;
using SimpleServiceBus.Utilities.ObjectBuilder;
using StructureMap;

namespace SimpleServiceBus.StructureMap
{
    /// <summary>
    /// A StructureMap implementation of IObjectBuilder. To see this in action, see the SampleManagedEndpoint sample in the Health Monitoring Samples.
    /// </summary>
    public class StructureMapObjectBuilder : IObjectBuilder
    {
        public T TryBuild<T>()
        {
            return (T) TryBuild(typeof (T));
        }

        public object TryBuild(Type typeToBuild)
        {
            return ObjectFactory.TryGetInstance(typeToBuild);
        }

        public T Build<T>()
        {
            return (T) Build(typeof (T));
        }

        public object Build(Type typeToBuild)
        {
            return ObjectFactory.GetInstance(typeToBuild);
        }

        public void RegisterSingleton<SingletonType>(SingletonType singleton)
        {
            ObjectFactory.Inject<SingletonType>(singleton);
        }
    }
}
